/*
* $Id: DBManager.java 266 2011-02-07 23:55:50Z volok-aleksej@yandex.ru $
*
* Copyright 2008-2011 by Volokitin Alexey
* All rights reserved
*
*/
package apiary.db.managers;

import java.util.Vector;
import java.util.Hashtable;

import apiary.db.ChangesStore;
import apiary.db.PointStore.PointRecord;
import apiary.db.FamilyStore.FamilyRecord;
import apiary.db.ChangesStore.ChangesRecord;
import apiary.db.HousingStore.HousingRecord;
import apiary.db.QueenBeeStore.QueenBeeRecord;
import apiary.db.managers.InspectManager.InspectManagerRecord;
import apiary.ui.journal.PointForm.PointValue;
import apiary.ui.journal.FamilyForm.FamilyValue;
import apiary.ui.journal.HousingForm.HousingValue;
import apiary.ui.journal.JournalForm.JournalValue;
import apiary.ui.journal.QueenBeeForm.QueenBeeValue;

/**
 * Main database manager.It combines all the managers.
 * All operation must be made throught it;
 * @author lexus
 */
public class DBManager
{
    /** Manager of point database  */
    private PointManager pointManager;
    /** Manager of user's fields value database */
    private FieldsValueManager fvManager;
    /** Manager of family database */
    private FamilyManager familyManager;
    /** Manager of hive database */
    private HousingManager housingManager;
    /** Manager of user's fields database */
    private UserFieldsManager ufManager;
    /** Manager of queen bees database */
    private QueenBeeManager qbManager;
    /** Manager of inspect database */
    private InspectManager inspectManager;

    /** Changes database */
    private ChangesStore changesStore;

    /** Instance class */
    private static DBManager instance = null;
    /**
     * Gets instance class
     * @return instance class
     */
    public static DBManager getInstance() {
        if(instance == null)
            instance = new DBManager();
        return instance;
    }

    /**
     * Constructor
     */
    public DBManager() {
        ufManager = new UserFieldsManager();
        fvManager = new FieldsValueManager();
        pointManager = new PointManager();
        familyManager = new FamilyManager();
        housingManager = new HousingManager();
        qbManager = new QueenBeeManager();
        inspectManager = new InspectManager();

        changesStore = new ChangesStore();
        readChanges();
    }

//---------------------------db manipulation function---------------------------
    /**
     * Adds point.
     * Add in changes database.
     * Add at list of point in manager of it.
     * @param value point
     */
    public void addPoint(PointValue value) {
        byte[] changeData = pointManager.addPoint(value);
        if(changeData != null)
            changesStore.addChange(ChangesRecord.TYPE_ADD_POINT, changeData);
    }
    /**
     * Changes point
     * Add in changes database.
     * Change at list of point in manager of it.
     * @param id identificator of point
     * @param point point
     */
    public void changePoint(int id, PointValue point) {
        byte[] changeData = pointManager.changePoint(id, point);
        if(changeData != null)
            changesStore.addChange(ChangesRecord.TYPE_CHANGE_POINT, changeData);
    }
    /**
     * Delete point
     * Add in changes database.
     * Deletes from list of point in manager of it.
     * @param id identificator of point
     */
    public void deletePoint(int id) {
        byte[] changeData = pointManager.deletePoint(id);
        if(changeData != null)
            changesStore.addChange(ChangesRecord.TYPE_DELETE_POINT, changeData);
    }
    /**
     * Adds family.
     * Add in changes database.
     * Add at list of family in manager of it.
     * @param value family
     */
    public void addFamily(FamilyValue value) {
        byte[] changeData = familyManager.addFamily(value);
        if(changeData != null)
            changesStore.addChange(ChangesRecord.TYPE_ADD_FAMILY, changeData);
    }
    /**
     * Changes family
     * Add in changes database.
     * Change at list of family in manager of it.
     * @param id identificator of family
     * @param value family
     */
    public void changeFamily(int id, FamilyValue value) {
        byte[] changeData = familyManager.changeFamily(id, value);
        if(changeData != null)
            changesStore.addChange(ChangesRecord.TYPE_CHANGE_FAMILY, changeData);
    }
    /**
     * Delete family
     * Add in changes database.
     * Deletes from list of family in manager of it.
     * @param id identificator of family
     */
    public void deleteFamily(int id) {
        byte[] changeData = familyManager.deleteFamily(id);
        if(changeData != null)
            changesStore.addChange(ChangesRecord.TYPE_DELETE_FAMILY, changeData);
    }
    /**
     * Adds inspect.
     * Add in changes database.
     * Add at list of inspects in manager of it.
     * @param idFamily identificator of family
     * @param value common value of inspect
     * @param inspectH list of hive's inspects
     */
    public void addInspect(int idFamily, JournalValue value, Vector inspectH) {
        byte[] changeData = inspectManager.addInspect(idFamily, value, inspectH);
        if(changeData != null)
            changesStore.addChange(ChangesRecord.TYPE_ADD_INSPECT, changeData);
    }
    /**
     * Changes inspect
     * Add in changes database.
     * Changes at list of inspects in manager of it.
     * @param idJournal identificator of inspect
     * @param value common value of inspect
     * @param inspectH list of hive's inspects
     */
    public void changeInspect(int idJournal, JournalValue value, Vector inspectH) {
        byte[] changeData = inspectManager.changeInspect(idJournal, value, inspectH);
        if(changeData != null)
            changesStore.addChange(ChangesRecord.TYPE_CHANGE_INSPECT, changeData);
    }
    /**
     * Delete inspect
     * Add in changes database.
     * Deletes fron list of inspects in manager of it.
     * @param idJournal identificator of inspect
     */
    public void deleteInspect(int idJournal) {
        byte[] changeData = inspectManager.deleteInspect(idJournal);
        if(changeData != null)
            changesStore.addChange(ChangesRecord.TYPE_DELETE_INSPECT, changeData);
    }
    /**
     * Adds hive.
     * Add in changes database.
     * Add at list of hive in manager of it.
     * @param value hive
     */
    public void addHousing(HousingValue value) {
        byte[] changeData = housingManager.addHousing(value);
        if(changeData != null)
            changesStore.addChange(ChangesRecord.TYPE_ADD_HOUSING, changeData);
    }
    /**
     * Changes hive.
     * Add in changes database.
     * Change at list of hive in manager of it.
     * @param value hive
     * @param id identificator of hive
     */
    public void changeHousing(int id, HousingValue value) {
        byte[] changeData = housingManager.changeHousing(id, value);
        if(changeData != null)
            changesStore.addChange(ChangesRecord.TYPE_CHANGE_HOUSING, changeData);
    }
    /**
     * Delete housing.
     * Add in changes database.
     * Deletes from list of hive in manager of it.
     * @param id identificator of queen bee
     */
    public void deleteHousing(int id) {
        byte[] changeData = housingManager.deleteHousing(id);
        if(changeData != null)
            changesStore.addChange(ChangesRecord.TYPE_DELETE_HOUSING, changeData);
    }
    /**
     * Adds queen bee.
     * Add in changes database.
     * Add at list of queen bee in manager of it.
     * @param value queen bee
     */
    public void addQueenBee(QueenBeeValue value) {
        byte[] changeData = qbManager.addQueenBee(value);
        if(changeData != null)
            changesStore.addChange(ChangesRecord.TYPE_ADD_QUEEN_BEE, changeData);
    }
    /**
     * Changes queen bee.
     * Add in changes database.
     * Change at list of queen bee in manager of it.
     * @param value queen bee
     * @param id identificator of queen bee
     */
    public void changeQueenBee(int id, QueenBeeValue value) {
        byte[] changeData = qbManager.changeQueenBee(id, value);
        if(changeData != null)
            changesStore.addChange(ChangesRecord.TYPE_CHANGE_QUEEN_BEE, changeData);
    }
    /**
     * Delete queen bee.
     * Add in changes database.
     * Deletes from list of queen bee in manager of it.
     * @param id identificator of queen bee
     */
    public void deleteQueenBee(int id) {
        byte[] changeData = qbManager.deleteQueenBee(id);
        if(changeData != null)
            changesStore.addChange(ChangesRecord.TYPE_DELETE_QUEEN_BEE, changeData);
    }
//-----------------------------get db content function--------------------------
    /**
     * Gets all points
     * @return list of points
     */
    public Vector getPoints() {
        return pointManager.getPoints();
    }
    /**
     * Gets point on identificator
     * @param id identificator of point
     * @return point
     */
    public PointRecord getPoint(int id) {
        return pointManager.getPoint(id);
    }
    /**
     * Gets user's fields values on identificator
     * @param idFieldValue identificator of user's fields values
     * @return list of user's fields values
     */
    public Vector getFieldsValue(int idFieldValue) {
        return fvManager.getFieldsValue(idFieldValue);
    }
    /**
     * Gets family on identificator
     * @param idFamily identificator of family
     * @return family
     */
    public FamilyRecord getFamily(int idFamily) {
        return familyManager.getFamily(idFamily);
    }
    /**
     * Gets families that are located in point
     * @param idPoint identificator of point
     * @return list of families
     */
    public Vector getFamilies(int idPoint) {
        return familyManager.getFamilies(idPoint);
    }
    /**
     * Gets user's fields for section
     * @param table field section
     * @return list of user's fields
     */
    public Vector getUserFields(int table) {
        return ufManager.getUserFields(table);
    }
    /**
     * Gets hives that is occupated family
     * @param idFamily identificator of family
     * @return list of hive
     */
    public Vector getHousings(int idFamily) {
        return housingManager.getHousings(idFamily);
    }
    /**
     * Gets hive on identificator
     * @param id identificator of hive
     * @return hive
     */
    public HousingRecord getHousing(int id) {
        return housingManager.getHousing(id);
    }
    /**
     * Gets queen bees that's used by family
     * @param idFamily identificator of family
     * @return list of queen bees
     */
    public Vector getQueenBees(int idFamily) {
        return qbManager.getQueenBees(idFamily);
    }
    /**
     * Gets inspects of family
     * @param idFamily identificator of family
     * @return list of inspects
     */
    public Vector getInspects(int idFamily) {
        return inspectManager.getInspects(idFamily);
    }
    /**
     * Gets inspect of family
     * @param id identificator of inspect
     * @return inspect
     */
    public InspectManagerRecord getInspect(int id) {
        return inspectManager.getInspect(id);
    }
    /**
     * Gets queen bees
     * @param id identificator of family
     * @return list of queen bees
     */
    public QueenBeeRecord getQueenBee(int id) {
        return qbManager.getQueenBee(id);
    }
    /**
     * Gets types of family
     * @return list of types
     */
    public Hashtable getTypesFamily(){
        return familyManager.getTypes();
    }
    /**
     * Gets types of hive
     * @return list of types
     */
    public Hashtable getTypesHousing() {
        return housingManager.getTypesHousing();
    }
    /**
     * Gets types of frame
     * @return list of types
     */
    public Hashtable getTypesFrame() {
        return housingManager.getTypesFrame();
    }
    /**
     * Gets breeds of bee
     * @return list of breeds
     */
    public Hashtable getBreeds() {
        return qbManager.getBreeds();
    }
    /**
     * Gets queen bee state for inspect
     * @return list of states
     */
    public Hashtable getQBStates() {
        return inspectManager.getQBStates();
    }
    /**
     * Gets diseases bee
     * @return list of diseases
     */
    public Hashtable getDiseases() {
        return inspectManager.getDiseases();
    }

//-------------------------------read db function-------------------------------
    /**
     * Reads the change database and fills all data of managers
     */
    private void readChanges() {
        Vector changesEnum = changesStore.getRecords();
        for(int i = 0; i < changesEnum.size(); i++) {
            ChangesRecord record = (ChangesRecord) changesEnum.elementAt(i);
            if(record.type == ChangesRecord.TYPE_ADD_POINT) {
                pointManager.addPoint(record.value);
            } else if (record.type == ChangesRecord.TYPE_CHANGE_POINT) {
                pointManager.changePoint(record.value);
            } else if (record.type == ChangesRecord.TYPE_DELETE_POINT) {
                pointManager.deletePoint(record.value);
            } else if(record.type == ChangesRecord.TYPE_ADD_FAMILY) {
                familyManager.addFamily(record.value);
            } else if(record.type == ChangesRecord.TYPE_CHANGE_FAMILY) {
                familyManager.changeFamily(record.value);
            } else if(record.type == ChangesRecord.TYPE_DELETE_FAMILY) {
                familyManager.deleteFamily(record.value);
            } else if(record.type == ChangesRecord.TYPE_ADD_INSPECT) {
                inspectManager.addInspect(record.value);
            } else if(record.type == ChangesRecord.TYPE_CHANGE_INSPECT) {
                inspectManager.changeInspect(record.value);
            } else if(record.type == ChangesRecord.TYPE_DELETE_INSPECT) {
                inspectManager.deleteInspect(record.value);
            } else if(record.type == ChangesRecord.TYPE_ADD_HOUSING) {
                housingManager.addHousing(record.value);
            } else if(record.type == ChangesRecord.TYPE_CHANGE_HOUSING) {
                housingManager.changeHousing(record.value);
            } else if(record.type == ChangesRecord.TYPE_DELETE_HOUSING) {
                housingManager.deleteHousing(record.value);
            } else if(record.type == ChangesRecord.TYPE_ADD_QUEEN_BEE) {
                qbManager.addQueenBee(record.value);
            } else if(record.type == ChangesRecord.TYPE_CHANGE_QUEEN_BEE) {
                qbManager.changeQueenBee(record.value);
            } else if(record.type == ChangesRecord.TYPE_DELETE_QUEEN_BEE) {
                qbManager.deleteQueenBee(record.value);
            }
        }
    }
}
